<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\View;
use think\Validate;
use app\common\model\Admin as AdminModel;

class Index extends Controller
{
    //限制重复登录
    public function initialize()
    {
        if (session('?admin.id')) {
            $this->redirect('admin/home/index');
        }
    }

    //登录后台界面
    public function login()
    {
        if (request()->isAjax()) {
            $data     = [
                'username' => input('post.username'),
                'password' => input('post.password'),
            ];
            $validate = new Validate([
                'username' => 'require',
                'password' => 'require'
            ]);
            $return   = $validate->check($data);
            if (!$return) {
                $this->error($validate->getError());
            }
            $return = new AdminModel();
            $return = $return->login($data);
            if ($return == 1) {
                $this->success('登陆成功', 'admin/home/index');
            } else {
                $this->error('用户名或密码错误');
            }
        }
        if (strpos($_SERVER["HTTP_USER_AGENT"], "Mobile")) {
            $view['mobile'] = true;
        } else {
            $view['mobile'] = false;
        }
        $this->assign($view);
        return view('login');
    }


}
